<?php

namespace App\Transformers;

use Spatie\Permission\Models\Role;
use League\Fractal\TransformerAbstract;

class RoleTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['children', 'permissions'];

    public function transform(Role $role)
    {
        return [
            'id' => $role->id,
            'name' => $role->name,
            'parent_id' => $role->parent_id,
            'is_role' => $role->is_role,
            'created_at' => $role->created_at->toDateTimeString(),
            'updated_at' => $role->updated_at->toDateTimeString(),
        ];
    }

    public function includeChildren(Role $role)
    {
        $transformer = new self();
        $transformer->setDefaultIncludes(['children']);
        return $this->collection($role->children, $transformer);
    }

    public function includePermissions(Role $role){
        return $this->collection($role->permissions,new PermissionTransformer());
    }
}